﻿<?php
/**
 * 定义用户注册相关操作
 *
 * @todo
 * @author Judas <judasnow@gmail.com>
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 */
class user_signup_process{
	
	private $db ;
	private $request ;
	private $db_obj_user ; 
	private $db_obj_user_profile ;

	function __construct( $db , $request ){

		$this->db = $db ;
		$this->request = $request ;
		$this->db_obj_user = new db_obj_user( $db );
		$this->db_obj_user_profile = new db_obj_user_profile( $db ) ;
	}
	
	function do_signup(){
		
		if ( !$this->request || !$this->db ){
			return false ;
		}

		@$username = $this->request['username'] ;
		@$passwd = $this->request['passwd'];
		@$role = '0' ;
		@$email = $this->request['email'] ;
		$signup_time = date("Y-m-d");

		$this->db_obj_user->set( 'username' , $username );
		$this->db_obj_user->set( 'passwd' , md5($passwd) );
		$this->db_obj_user->set( 'role' , $role );
		$this->db_obj_user->set( 'email' , $email ) ;
		$this->db_obj_user->set( 'signup_time' , $signup_time );
		$this->db_obj_user->save();

		$this->post_signup();
		
		/**
		 * user profile
 		*/	
		$this->set_user_profile( $this->request ) ;

		return true ;
	}

	/**
	 *成功注册用户之后进行调用
	 */
	function post_signup(){

		$username = $this->request['username'] ;

		//建立相应用户的user_profile表中的一行记录
		$sql = "INSERT INTO user_profile SET username = '$username'" ;
		$res = $this->db->query( $sql ) ;
		if (PEAR::isError($res)) {
			throw new Exception( '建立用户详细数据表记录时错误:sql='.$sql.':'.$res->getMessage() );
		}

		//建立相关用户在提交记录表中的一条记录
		$sql = "INSERT INTO user_judge_statistics SET username = '$username'" ;
		$res = $this->db->query( $sql ) ;
		if (PEAR::isError($res)) {
			throw new Exception( '建立用户提交记录表记录时错误:sql='.$sql.':'.$res->getMessage() );
		}

		return true ;
	}

	/**
	 * 设置用户详细信息
	 *
	 */
	function set_user_profile( $request ){
	
		$this->db_obj_user_profile->load( 'username' , $this->request['username'] );

		@$nick = $this->request['nick'];
		@$sex = $this->request['sex'];
		@$location = $this->request['location'];
		@$motto = $this->request['motto'];

		$this->db_obj_user_profile->set( 'nick' , $nick );
		$this->db_obj_user_profile->set( 'sex' , $sex );
		$this->db_obj_user_profile->set( 'location' , $location );
		$this->db_obj_user_profile->set( 'motto' , $motto );
		$this->db_obj_user_profile->save();

	}
	
	/**
	 *更新用户普通信息
	 *
	 */
	function update( $k , $v ){
		
		$this->db_obj_user->set( $k , $v ) ;
		$this->db_obj_user->save();
	}
	
	/**
	 *更新用户详细信息
	 *
	 */
	function update_user_profile( $k , $v ){
		
		$this->db_obj_user_profile->set( $k , $v ) ;
		$this->db_obj_user_profile->save();
	}

	static function exist_user( $db , $username ){
		
		$sql = "SELECT * FROM user WHERE username = '$username'";
		$res = $db->query( $sql ) ;
		if (PEAR::isError($res)) {

			throw new Exception('测试用户名是否存在时错误:sql='.$sql.':'.$res->getMessage() );
		}
		
		$num = $res->NumRows();

		if( $num == 0 ){
			
			return false ;
		
		}else if( $num == 1 ){
			
			return true ;	
		}else{
			
			throw new Exception('user表中用户名存在重复'.$username );		
		
		}
		
	}

	static function delete_user( $db , $username ){
				
		$sql = "DELETE FROM user WHERE username = '$username'";
		$res = $db->query( $sql ) ;
		if (PEAR::isError($res)) {

			throw new Exception('删除用户失败:sql='.$sql.':'.$res->getMessage() );
		}

		$sql = "DELETE FROM user_profile WHERE username = '$username'";
		$res = $db->query( $sql ) ;
		if (PEAR::isError($res)) {

			throw new Exception('删除用户失败:sql='.$sql.':'.$res->getMessage() );
		}

		return true ;
	}
}
?>
